package com.credithc.customers.manager.service.impl;


import com.credithc.baseserv.core.service.RemoteAuthService;
import com.credithc.customers.base.constants.CustomerConstants;
import com.credithc.customers.base.factory.DictionaryFactory;
import com.credithc.customers.manager.configure.FilePropertiesConfig;
import com.credithc.customers.manager.service.DesensitizationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;


@Service
public class DesensitizationServiceImpl implements DesensitizationService {


    private final RemoteAuthService remoteAuthService;
    private final FilePropertiesConfig filePropertiesConfig;

    @Autowired
    public DesensitizationServiceImpl(RemoteAuthService remoteAuthService, FilePropertiesConfig filePropertiesConfig) {
        this.remoteAuthService = remoteAuthService;
        this.filePropertiesConfig = filePropertiesConfig;
    }

    private boolean getSysDeSetting() {
        Map<String, String> dictMap = DictionaryFactory.Dict.SYS_SETTINGS.getDictMap();
        if(dictMap == null || dictMap.isEmpty()){
            return true;
        }
        String val = dictMap.get("desensitization");
        if(StringUtils.isBlank(val)){
            return true;
        }
        return Integer.parseInt(val) == 1;
    }

    @Override
    public void desensitizationList(List list) {

        boolean sysDeSetting = getSysDeSetting();
        if(sysDeSetting){
            for (Object o : list) {
                desensitization(o);
            }
        }
/*        //isNoNeedAuth 是否需要auth权限认证,不填或为false表示用到auth,true表示不用auth服务
        if (!(filePropertiesConfig.isNoNeedAuth())) {
            boolean b = false;
            b = getFlag();
            if (!b) {
                for (Object o : list) {
                    desensitization(o);
                }
            }
        }*/
    }

    //
    private void desensitization(Object o) {
        Field f1 = null;
        try {
            desensitizationCardNo(o, "idCardNo");
        } catch (Exception e) {

        }
        try {
            desensitizationCardNo(o, "idCardNoOld");
        } catch (Exception e) {

        }
        try {

            desensitizationPhone(o, "contactInfo");
        } catch (Exception e) {
            //e.printStackTrace();
        }
        try {
            desensitizationPhone(o, "contactInfoOld");
        } catch (Exception e) {
            // e.printStackTrace();
        }

    }

    private void desensitizationCardNo(Object o, String card) throws NoSuchFieldException, IllegalAccessException {
        Field f1;
        f1 = o.getClass().getDeclaredField(card);
        f1.setAccessible(true);
        String str = "";
        String idCardNo = (String) f1.get(o);
        if (idCardNo.length() >= 10) {
            String pre = idCardNo.substring(0, 3);
            str += pre;
            String tail = idCardNo.substring(idCardNo.length() - 4, idCardNo.length());
            for (int i = 3; i < idCardNo.length() - 4; i++) {
                str += "*";
            }
            str += tail;
        } else {
            String pre = idCardNo.substring(0, 1);
            str += pre;
            String tail = idCardNo.substring(idCardNo.length() - 1, idCardNo.length());
            for (int i = 1; i < idCardNo.length() - 1; i++) {
                str += "*";
            }
            str += tail;
        }
        //修改这个字段的值
        f1.set(o, str);
    }

    @Override
    public void desensitizationObject(Object o) {
        boolean sysDeSetting = getSysDeSetting();
        if(sysDeSetting){
            desensitization(o);
        }


        /*if (!(filePropertiesConfig.isNoNeedAuth())) {
            boolean b = false;
            b = getFlag();
            if (!b) {
                desensitization(o);
            }
        }*/
    }

    private void desensitizationPhone(Object o, String str) throws NoSuchFieldException, IllegalAccessException {
        Field f2 = o.getClass().getDeclaredField(str);
        //尝试获取字段中的值

        f2.setAccessible(true);
        String contactInfo = (String) f2.get(o);
        String newContactInfo = contactInfo.substring(0, 3);
        for (int j = 3; j < contactInfo.length(); j++) {
            newContactInfo += "*";
        }
        f2.set(o, newContactInfo);
        //查看这个字段的值
    }

    private boolean getFlag() {
        boolean flag = false;
        List<String> accesssList = remoteAuthService.getDataBySystemIdAndDataTypeId(CustomerConstants.SYSTEM_NAME, CustomerConstants.DATA_TYPE_NAME);
        if (accesssList != null && accesssList.size() > 0 && (!accesssList.get(0).equals("A0"))) {
            flag = true;
        }
        return flag;
    }
}
